package com.bana.fvs.protocol;

import com.bana.fvs.model.ControllerText;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * 查询控制器基本信息
 * 开始地址，结束地址，巡检间隔时间
 */
public class QueryControllerText {

    public int getId()
    {
        return 0x33;
    }

    public byte[] toBuffer(int kzqbsm,int lxbh)
    {
        byte[] buffer=new byte[5];
        buffer[0]=(byte)0x81;
        buffer[1]=(byte)kzqbsm;
        buffer[2]=(byte)getId();
        buffer[3]=(byte)lxbh;
        buffer[4]=(byte)0xff;
        return buffer;
    }

    public ControllerText toModel(byte[] buffer) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        DataInputStream dis = new DataInputStream(bais);
        dis.skipBytes(1);
        dis.skipBytes(1);
        dis.skipBytes(1);
        ControllerText controllerText =new ControllerText();
        controllerText.setLxbh(dis.readByte());

        byte[] textBuff=new byte[buffer.length-5];
        for(int i=0;i<buffer.length-5;++i)
        {
            textBuff[i]= (byte) (dis.readByte()+0xA0);
        }
        controllerText.setWdwz(new String(textBuff,"GB2312"));

        return controllerText;
    }
}
